package com.song.shop.controller;

import com.song.shop.redis.RedisDistributed;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by IntelliJ IDEA.
 *
 * @author: Song Yinhui
 * @Date: 2018/11/28 17:48
 */
@RestController
public class RedisControlelr {

    private Logger logger = LoggerFactory.getLogger(RedisControlelr.class);
    @Autowired
    private RedisTemplate redisTemplate ;



    /*public RedisControlelr() {
        RedisDistributed redisDistributed = new RedisDistributed(redisTemplate, "songying", String.valueOf(60 * 20));
    }*/
    @RequestMapping(value = "lock")
    public String test(){
        RedisDistributed redisDistributed = new RedisDistributed(redisTemplate , "songying" , String.valueOf(60*20));
        Boolean lock = redisDistributed.lock();
        if (lock){
            logger.info("这次加锁成功");
        }else {
            logger.info("这次加锁失败");
        }
        return "success";
    }

    @RequestMapping(value = "release")
    public String release(){
        RedisDistributed redisDistributed = new RedisDistributed(redisTemplate , "songying" , String.valueOf(60*20));
        Boolean release = redisDistributed.release();
        if (release){
            logger.info("这次释放锁成功");
        }else {
            logger.info("这次释放失败");
        }
        return "success";

    }
}
